package com.example.demo.gateway.config;

import com.example.demo.gateway.core.limiter.DynamicRateLimiter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter;
import org.springframework.cloud.gateway.support.ConfigurationService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;

import java.util.List;

/**
 * @Auther: miaoguoxin
 * @Date: 2020/3/17 23:06
 * @Description:
 */
@Configuration
public class RateLimiterConfiguration {
    @Bean
    @ConditionalOnMissingBean
    public DynamicRateLimiter redisRateLimiter(ReactiveStringRedisTemplate redisTemplate,
                                             @Qualifier(RedisRateLimiter.REDIS_SCRIPT_NAME) RedisScript<List<Long>> redisScript,
                                             ConfigurationService configurationService) {
        return new DynamicRateLimiter(redisTemplate, redisScript, configurationService);
    }
}
